package com.example.demo.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
public class TimeAspect {

    @Pointcut("execution(* com.example.demo.service.*.*(..))")
    private void pc(){}

    @Before("@annotation(com.example.demo.aop.MyLog)")
    public void runBefore1(JoinPoint joinPoint){
        log.info("自定义注解的{}方法开始执行了", joinPoint.getSignature());
    }

//    @Around("execution(* com.example.demo.service.*.*(..))")
    @Around("pc()")
    public Object recordTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        long begin = System.currentTimeMillis();
        Object obj = proceedingJoinPoint.proceed();
        long end = System.currentTimeMillis();
        log.info(proceedingJoinPoint.getSignature() + "执行时间：{}ms" + (end - begin));

        return obj;
    }

//    @Before("execution(* com.example.demo.service.*.*(..))")
    @Before("pc()")
    public void runBefore(){
        log.info("现在是方法执行前");
    }

//    @After("execution(* com.example.demo.service.*.*(..))")
    @After("pc()")
    public void runAfter(){
        log.info("现在是方法执行后");
    }

    @AfterReturning("pc()")
    public void runTrueAfter(){
        log.info("正常运行了");
    }

    @AfterThrowing("pc()")
    public void runFalseAfter(){
        log.info("异常运行了");
    }
}
